#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

char *find_space(char *st) {
    while (*st && *st != '\0') {
        if (*st == ' ') {
            return st;
        }
        st++;
    }
    return NULL;
}


char *s_gets(char *st, int n) {
    char *ret_val;
    ret_val = fgets(st, n, stdin);
    if (ret_val) {
        while (*st != '\n' && *st != '\0') {
            ++st;
        }
        if (*st == '\n') {
            *st = '\0';
        } else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}
//
//int strlen(char *s) {
//    int sum = 0;
//    while (*s) {
//        sum++;
//        s++;
//    }
//    return s;
//}

char *s_gets1(char *st, int n) {
    char *ret_val;
    ret_val = fgets(st, n, stdin);
    if (ret_val) {
        char *ptr;
        if ((ptr = strchr(st, '\n')) != NULL) {
            *ptr = '\0';
        } else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

void p1(char *str, int n) {
    int i;
    for (i = 0; i < n - 1; ++i) {
        *(str++) = (char) getchar();
    }
    *str = '\0';
    //去除多余字符
    while (getchar() != '\n')
        continue;
}


//用scanf
void p21(char *str) {
    //有内存溢出的风险
    scanf("%s", str);
}

//用 getchar
void p22(char *str, int n) {
    char now;
    for (int i = 0; i < n - 1; ++i) {
        now = (char) getchar();
        if (isspace(now)) {
            break;
        }
        *(str++) = now;
    }
    *str = '\0';
}

//p3略
char p4(char*st,int n){
    char now;
    for (int i = 0; i < n - 1;) {
        now = (char) getchar();
        //若遇到EOF返回空指针
        if (now == EOF) return NULL;
        if (i==0 && isspace(now)) continue;
        else i++;
        if (isspace(now)) {
            break;
        }
        *(st++) = now;
    }
    *st = '\0';
}
//p5 找字符出现的位置
char * mystrchr(char c, char * st) {
    while (*st!='\0'){
        if (*st == c) return st;
        st++;
    }
    return NULL;
}

//p6
int is_with_in(char c, const char * st) {
    char now;
    while ((now = *(st++))!='\0'){
        if (now == c) return 1;
    }
    return 0;
}

//p7，复制字符串
char *mystrncpy(char *s1, char *s2, int n) {
    for (int i = 0; i < n; ++i) {
        s1[i] = s2[i];
        if (s2[i]=='\0') {
            break;
        }
    }
    return s1;
}
//p8 找字符串首次出现的位置
char * string_in(char *search,char *text){
    int length1=0;
    int length2=0;
    char * ptr_s = search;
    char * ptr_t = text;
    while (*(ptr_s++)!='\0'){
        length1++;
    }
    while (*(ptr_t++)!='\0'){
        length2++;
    }
    if (length1 < 1 || length2 < 1 || length1 > length2) {
        return NULL;
    }
    int current_pos = 0;
    while (current_pos + length1 <= length2 ){
        ptr_t = text + current_pos;
        ptr_s = search;
        bool match = true;
        char s;
        while ((s=*(ptr_s++))!='\0'){
            if (s!=*(ptr_t++)){
                match = false;
            }
        }
        if (match)
            return text + current_pos;
        current_pos++;
    }
    return NULL;
}
//p9 将字符串倒序排列,不能传递char*
char * revert_string(char st[]){
    char *start = &st[0];
    //查找末尾字符
    char *end = &st[0];
    while (*end!='\0')
        end++;
    //移到'\0'之前
    *start = 'c';
    end--;
    //交换位置
    for(;end!=start&&end!=start+1;end--,start++){
        char temp1 = *start;
        *start = *end;
        *end = temp1;

    }
    return st;
}
//翻转int可用指指针
//int * revert_int(int *st,int n){
//    int *start = &st[0];
//    //查找末尾字符
//    int *end = &st[n-1];
//    //交换位置
//    for(;end!=start&&end!=start+1;end--,start++){
//        int temp = *start;
//        *start = *end;
//        *end = temp;
//    }
//    return st;
//}

//p10 读取知道空行的输入，删除每行的空格
char * delete_space(char st[]){
    while (*st!='\0'){
        if (*st == ' '){
            char * ptr = st;
            char * ptr1 = st+1;
            for (; *ptr1 !='\0' ; ptr++,ptr1++) {
                *ptr = *ptr1;
            }
            //末尾设置为字符串结尾
            *ptr = '\0';
        }
        st++;
    }
    return st;
}
#define P10_SIZE 100
void p10(){
    char st[P10_SIZE];
    while (*fgets(st, P10_SIZE, stdin)!='\n') {
        delete_space(st);
        fputs(st, stdout);
    }
}

void p12(){
    char ch;
    int sum_upper = 0;
    int sum_lower = 0;
    int sum_word = 0;
    int sum_punctuation = 0;
    int sum_number = 0;
    char *last_ch;
    int sum_char= 0;
    while ((ch = getchar()) != EOF) {

        if (isupper(ch)) {
            sum_upper++;
        } else if (islower(ch)) {
            sum_lower++;
        } else if (ispunct(ch)) {
            sum_punctuation++;
        } else if (isnumber(ch)) {
            sum_number++;
        } else if(isspace(ch)&&sum_char!=0&&!isspace(last_ch)){
           sum_word++;
        }

        sum_char++;
        last_ch = ch;
    }
    printf("sum_upper = %d\n", sum_upper);
    printf("sum_lower = %d\n", sum_lower);
    printf("sum_punctuation = %d\n", sum_punctuation);
    printf("sum_number = %d\n", sum_number);
    printf("sum_word = %d\n", sum_word);
}

void p13(){
    char cc[100];
    char *val = s_gets(cc, 100);
    if (val){
        int length = strlen(cc);
        for (int i = length-1; i >=0 ; --i) {
            putchar(val[i]);
        }
    } else{
        puts("输入有误。");
    }

}
double power(double x, int y) {
    double result = 1;
    if (x == 0) {
        if (y == 0) {
            printf("0的0次幂未定义。\n");
        } else {
            result = 0;
        }
    } else {
        if (y > 0) {
            for (int i = 0; i < y; ++i) {
                result *= x;
            }
        } else if (y < 0) {
            for (int i = 0; i < -y; ++i) {
                result *= 1 / x;
            }
        }
    }
    return result;
}

void p14(){
    double ds;
    int zs;
    printf("输入底数：");
    int val = scanf("%lf", &ds);
    if(val!=1){
        printf("输入错误。");
        return;
    }
    printf("输入指数：");
    val = scanf("%d", &zs);
    if(val!=1){
        printf("输入错误。");
        return;
    }
    double rst = power(ds,zs);
    printf("%.3f^%d=%.3f", ds, zs,rst);
}

int  myatoi(char *st) {
    int val = 0;
    for (int i = 0; *st != '\0'; ++i, st++) {
        if (isnumber(*st)) {
            val = val * 10 + (*st-48);
        } else{
            return 0;
        }
    }
    return val;
}
void p15(){
    char *sts[5] = {
            "123",
            "ab1",
            "1ab",
            "3",
            ""
    };
    for (int i = 0; i < 5; ++i) {
        printf("%s to i = %d\n", sts[i], myatoi(sts[i]));
    }
}

int main() {
//    char str[20];
//    p1(str, 20);
//    p22(str,20);
//    p4(str, 20);
//    for (int i = 0; i < 20; ++i) {
//        if (str[i] != '\0')
//            putchar(str[i]);
//        else{
//            break;
//        }
//    }
//p5
//    char *ptr = mystrchr('c', "acbca");
//p6
//    const char st[] = "12ab34ghpqZSL";
//    for (char i = 'a'; i <= 'z'; ++i) {
//        if(is_with_in(i,st)){
//            printf("the '%c' at st\n", i);
//        }
//    }
//p7
//    const int length = 12;
//    char target_p7[length];
//    mystrncpy(target_p7, "abcdef", 12);
//    for (int i = 0; i < length; ++i) {
//        char c = target_p7[i];
//        if (c != '\0') {
//            putchar(c);
//        } else{
//            break;
//        }
//    }
//p8
//    char * ptr = string_in("c", "abac");
//p9
//    char  str[] = "abcdef";
//    char * ptr =revert_string(str);
//        int arr[] = {1,2,3,4,5};
        //反转int有效
//    revert_int(arr, 5);
//    p10();
//    p12();
//    p13();
//    p14();
    p15();
    return 0;
}

